/*
 * Created on Sep 30, 2003
 * DesktopAction
 * Local Projects Database
 * (c) 2003 eRomania Gateway Association
 *
 */
package ro.gateway.aida.struts.action;

import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.actions.TilesAction;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.db.SearchDB;
import ro.gateway.aida.usr.Arbiter;
import ro.gateway.aida.usr.Perms;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.BugTracker;
import ro.gateway.aida.utils.PageToken;
import ro.gateway.aida.utils.SortedList;
import ro.gateway.aida.utils.Utils;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;


/**
 * @author Mihai Postelnicu
 * DesktopAction
 *
 *  *
 */
public class DesktopAction extends TilesAction {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) {
        try {
            ServletContext sc = getServlet().getServletContext();
            PersistenceToken token = PersistenceManager.tokenLookup(sc);
            User user = UserUtils.getLogged(request, response);
            Arbiter arb = Arbiter.getManager(token);
            HttpSession session = request.getSession();

            if (user == null)
                return mapping.findForward("plslogin");
//TODO: searchdb should ALWAYS be created here. 
//Maybe we need to move it to another bean 
            SearchDB sdb = SearchDB.getManager(token);
            LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage(request,
                    sc);
            String lang = lang_bean.language;
            String mode = request.getParameter("mode");
            String sort = request.getParameter("sort");

            SortedList ls = (SortedList) session.getAttribute(
                    "prjSearchResults");

            if ("countrySort".equals(sort)) {
                SortedList resorted = sdb.reorder("country", ls);
                session.setAttribute("prjSearchResults", resorted);
            }

            if ("sectorSort".equals(sort)) {
                SortedList resorted = sdb.reorder("sector", ls);
                session.setAttribute("prjSearchResults", resorted);
            }

            if ("titleSort".equals(sort)) {
                SortedList resorted = sdb.reorder("title", ls);
                session.setAttribute("prjSearchResults", resorted);
            }

            //get the activity search
            sdb.setUserID(user.getId());
            sdb.setAttachLocations(true);
            sdb.setAttachSectors(true);
            sdb.setSortByTitle(false);
            sdb.setSearchLang(lang);

            SortedList prj;

            if ((sort == null) && ("firstlist".equals(mode) || (mode == null))) {
                prj = sdb.advancedSearch();
                session.setAttribute("prjSearchResults", prj);

                if (mode != null)
                    mode = "projectlist";
            } else
                prj = (SortedList) session.getAttribute("prjSearchResults");

            if (arb.canDo(request, Perms.OBJECT_SUBMIT))
                request.setAttribute("addlinks", "addlinks");

            if ("projectlist".equals(mode)) {
               	List pagetk = PageToken.makePageTokens(prj, token);

                if (pagetk != null) {
                    Iterator i = pagetk.iterator();

                    while (i.hasNext()) {
                        PageToken element = (PageToken) i.next();
                        element.getParams().put("mode", "projectlist");
                    }

                    request.setAttribute("pagetk", pagetk);
                }

                session.setAttribute("prjPageSearchResults",
                    PageToken.handleCurrent(request, sc, prj));
                Utils.setBreadCrumbs(request, "bc.project.list");

                return mapping.findForward("projectlist");
            } else {
                session.setAttribute("prjSearchResults", prj);
                Utils.setBreadCrumbs(request, "bc.desktop");

                return mapping.findForward("desktop");
            }
        } catch (Exception e) {
            BugTracker.publishBug(e, request);

            return mapping.findForward("fail");
        }
    }
}
